//
//  HomeTableViewController.swift
//  Weibo
//
//  Created by 冯童童 on 16/1/11.
//  Copyright © 2016年 冯童童. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

private let HomeCellID = "HomeCellID"
class HomeTableViewController: BaseTableViewController {

    private lazy var listViewModel = StatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if !userLogin {
            visitorLoginView?.setupUIWithInfo(nil, tipText: "关注一些人，也许获得到意想不到的惊喜")
            return  
        }
        
        prepareTableView()
        /*
        loadData { (statuses) -> () in
            guard let list = statuses else {
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                    return
            }
            self.statuses = list
            self.tableView.reloadData()
        }
        //demo()
        更改方法
        */
        loadData()
    }
    
    private func prepareTableView() {
        self.tableView.registerClass(StatusCell.self, forCellReuseIdentifier: HomeCellID)
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.estimatedRowHeight = 300
        tableView.separatorStyle = .None
        
    
        self.tableView.addSubview(myRefresh)
        myRefresh.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
//        refreshControl = UIRefreshControl()
//        
//        refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        tableView.tableFooterView = indicatorView
    }
    
  @objc private func loadData() {
    
    listViewModel.loadHomeData(indicatorView.isAnimating()) { (isSuccess) -> () in
        self.myRefresh.endRefreshing()
        self.refreshControl?.endRefreshing()
        if !isSuccess {
            SVProgressHUD.showErrorWithStatus(AppErrorTip)
            return
        }
        
        if self.indicatorView.isAnimating() {
            
            self.indicatorView.stopAnimating()
        }
        
        if !self.tableView.dragging {
            self.tableView.reloadData()

        }
    }
    }
    private lazy var indicatorView : UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)

}

extension HomeTableViewController {
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.listViewModel.statuses.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellID, forIndexPath: indexPath) as! StatusCell
        //cell.textLabel?.text = self.statuses[indexPath.row].user?.name
        cell.status = self.listViewModel.statuses[indexPath.row]
        //print(statuses[indexPath.row])
        if indexPath.row == self.listViewModel.statuses.count - 1 && !indicatorView.isAnimating(){
            indicatorView.startAnimating()
            loadData()
            print("开始静默加载数据")
        }
        print(self.listViewModel.statuses[indexPath.row].retweeted_status?.text)
        return cell
    }
}



